package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description
 * @Author klw
 * @Data 2022/11/20 下午6:56
 */

@Service
public class CommentServiceImpl implements CommentService{

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Override
    public CommonData<MarketComment> query(Integer userId, Integer valueId, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        marketCommentExample.setDistinct(true);
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();

        if(userId != null && !"".equals(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        if(valueId != null && !"".equals(valueId)){
            criteria.andValueIdEqualTo(valueId);
        }
        criteria.andDeletedEqualTo(0);

        marketCommentExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        PageInfo pageInfo = new PageInfo(marketComments);

        return CommonData.data(pageInfo);
    }

    @Override
    @Transactional
    public void delete(MarketComment marketComment) {
        Integer id = marketComment.getId();
        marketCommentMapper.deleteById(id);
    }

    @Override
    public int reply(Integer commentId, String content) {
        String content1 = marketCommentMapper.selectIfExistedContent(commentId);
        if(content1 != null && !"".equals(content1)){
            return 622;
        }
        marketCommentMapper.updateContent(commentId,content);
        return 0;
    }
}
